'''
人人安康家庭医疗保险
'''

import sys

sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import execjs


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "人人安康家庭医疗保险"

    # 生成产品数据
    def genProductInfo(self):

        plan = {

            # 年龄段
            "year": [str(i) for i in range(0, 80)],
            # 是否有社保
            "socialSec": ["Y", "N"],
            # 保障计划
            "planCode": ["0", "1"],
            # 保险期间
            "expire": ["1年"]
        }
        for v in product(plan["year"], plan["socialSec"], plan["planCode"], plan["expire"]):

            # 写入数据库
            if v[2] == "0":

                plan = "普惠款"

            else:

                plan = "精选款"

            self.mysql.cursor.execute(domain[self.name]["insert_sql"].format(v[0], v[1], v[2], plan, v[3]))

            self.mysql._conn.commit()

            print(domain[self.name]["insert_sql"].format(v[0], v[1], v[2], plan, v[3]))

    # 计算保险总价
    def calcInsureTotal(self, dt=None):

        data = {

            "day": int(dt["year"]),
            "plan": int(dt["planCode"]),
            "index": 0 if dt["socialSec"] == "Y" else 1

        }


        price = self.scriptJs( data )

        data = {

            "name":self.name,
            "id":dt["id"],
            "price":price
        }
        self.mysql.updatePrice( data )


    # 设置JS算法
    def scriptJs(self,data):

        js = '''
        function CalculationFee(data){
        
            var list = [
                {
                    '0-4':[488,1078],
                    '5-10':[246,498],
                    '11-15':[148,268],
                    '16-20':[86,146],
                    '21-25':[146,298],
                    '26-30':[186,406],
                    '31-35':[256,576],
                    '36-40':[316,728],
                    '41-45':[406,1126],
                    '46-50':[598,1586],
                    '51-55':[766,2366],
                    '56-60':[976,3018],
                    '61-65':[1308,4058]
                },
                {
                    '0-4':[796,1780],
                    '5-10':[396,798],
                    '11-15':[236,436],
                    '16-20':[136,236],
                    '21-25':[238,496],
                    '26-30':[298,658],
                    '31-35':[406,908],
                    '36-40':[506,1206],
                    '41-45':[636,1836],
                    '46-50':[988,2618],
                    '51-55':[1258,3906],
                    '56-60':[1608,4990],
                    '61-65':[2165,6711]
                }
            ];
             for (var key in list[data["plan"]]){
                var keySplit = key.split("-");
                if(data["day"]>=keySplit[0]&&data["day"]<=keySplit[1]){
                   price = list[data["plan"]][key][data["index"]].toFixed(2)
                   return price
        }
    }
        }
        '''

        cxt = execjs.compile(js)

        price = cxt.call('CalculationFee',data)

        return price

    def main(self):

        while True:

            # 随机领取一条数据
            dt = self.mysql.randTask(self.name)

            if not dt:
                print("产品价格抓取完成~~~")

                return

                # 任务执行状态
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 1
            }
            self.mysql.updateTask(data)

            self.calcInsureTotal(dt)
